package com.cookor.poi.common.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HtmlUtils {

    private static PropertiesLoader loader = new PropertiesLoader("jeesite.properties");

    /** 前后端分离项目，图片带上后台服务器地址*/
    public static String changeImgPath (String newsBody) {
        Element doc = Jsoup.parseBodyFragment(newsBody).body();
        Elements pngs = doc.select("img[src]");
        String httpHost = "https://zx.nthink.cn";
        for (Element element : pngs) {
            String imgUrl = element.attr("src");
            if (imgUrl.trim().startsWith("/")) { // 会去匹配我们富文本的图片的 src 的相对路径的首个字符，请注意一下
                imgUrl =httpHost + imgUrl;
                element.attr("src", imgUrl);
            }
        }

        Elements as = doc.select("a[href]");
        String url;
        String substring;
        for (Element element : as) {
            url = element.attr("href");
            substring = url.substring(0, 1);
            if(substring.equals("/")){
                url = httpHost + url;
                element.attr("href", url);
            }
        }

        return newsBody = doc.toString();
    }



}
